package org.tpc.yn.utils;

import lombok.extern.slf4j.Slf4j;
import net.sourceforge.pinyin4j.PinyinHelper;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class StringUtil {
//    public static String subStrSQL(String val){
//        if(val.contains("ORA-20001")){
//            String[] split = val.split("ORA-20001:");
//            String s = split[split.length - 1];
//            return s.substring(0,s.indexOf("ORA"));
//        }
//        return val.substring(val.indexOf(":")+1,val.indexOf("ORA",val.indexOf(":")));
//    }

    public static String subStrSQL(String val){
        log.info("====:{}",val);
        Pattern pattern = Pattern.compile("ORA-\\d+:\\s*(.*?)\\n");
        Matcher matcher = pattern.matcher(val);
        if (matcher.find()) {
            String errorString = matcher.group(1);
//            System.out.println(errorString);
            return errorString;
        }else{
            return val;
        }
    }

    public static String getPlaceholders(int count) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < count; i++) {
            if (i > 0) {
                sb.append(", ");
            }
            sb.append(" ?");
        }
        return sb.toString();
    }

    public static String getPinyinString(String chineseStr) {
        StringBuilder pinyinStr = new StringBuilder();
        for (char c : chineseStr.toCharArray()) {
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c);
            if (pinyinArray != null) {
                pinyinStr.append(pinyinArray[0]); // 获取第一个拼音
            } else {
                pinyinStr.append(c); // 如果不是汉字，则保留原字符
            }
        }
        return pinyinStr.toString();
    }

}
